import type * as grpc from "@grpc/grpc-js";

export type GrpcCall = grpc.UntypedHandleCall;

interface handleUnaryServiceImplementation {
  [name: string]: grpc.UntypedHandleCall;
}

export type RequestHandleUnaryCall<T extends grpc.UntypedHandleCall> =
  T extends grpc.handleUnaryCall<infer Request, any> ? Request : never;

export type ReplyHandleUnaryCall<T extends grpc.UntypedHandleCall> =
  T extends grpc.handleUnaryCall<any, infer Reply> ? Reply : never;

export type MayPromise<T> = Promise<T> | T;

export type PromiserHandlers<U extends handleUnaryServiceImplementation> = {
  [m in keyof U]: (
    request: RequestHandleUnaryCall<U[m]>
  ) => MayPromise<ReplyHandleUnaryCall<U[m]>>;
};
